<template>
  <div class="profile">
    <div class="page-header">
      <h2>个人中心</h2>
      <p>管理您的个人信息</p>
    </div>
    
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card class="profile-card">
          <div class="profile-avatar">
            <el-avatar :size="100" :src="userInfo.avatar">
              <el-icon><User /></el-icon>
            </el-avatar>
            <h3>{{ userInfo.realName || userInfo.username }}</h3>
            <p>{{ getUserTypeText(userInfo.userType) }}</p>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="16">
        <el-card class="info-card">
          <h3>基本信息</h3>
          <el-form :model="userInfo" label-width="100px" v-loading="loading">
            <el-form-item label="用户名">
              <el-input v-model="userInfo.username" disabled />
            </el-form-item>
            <el-form-item label="真实姓名">
              <el-input v-model="userInfo.realName" />
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="userInfo.phone" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="userInfo.email" />
            </el-form-item>
            <el-form-item label="地址">
              <el-input v-model="userInfo.address" type="textarea" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="updateProfile" :loading="updateLoading">
                保存修改
              </el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row style="margin-top: 20px;">
      <el-col :span="24">
        <el-card>
          <h3>修改密码</h3>
          <el-form :model="passwordForm" :rules="passwordRules" ref="passwordFormRef" label-width="100px">
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="passwordForm.newPassword" type="password" show-password />
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input v-model="passwordForm.confirmPassword" type="password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="changePassword" :loading="passwordLoading">
                修改密码
              </el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import { userApi } from '@/api/user'

export default {
  name: 'Profile',
  data() {
    return {
      userInfo: {},
      loading: false,
      updateLoading: false,
      passwordLoading: false,
      passwordForm: {
        newPassword: '',
        confirmPassword: ''
      },
      passwordRules: {
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, message: '密码至少6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.validatePasswordConfirm, trigger: 'blur' }
        ]
      }
    }
  },
  
  mounted() {
    this.loadUserInfo()
  },
  
  methods: {
    validatePasswordConfirm(rule, value, callback) {
      if (value !== this.passwordForm.newPassword) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    },
    
    async loadUserInfo() {
      this.loading = true
      try {
        const response = await userApi.getCurrentUser()
        if (response.success) {
          this.userInfo = { ...response.data }
        }
      } catch (error) {
        console.error('加载用户信息失败:', error)
        ElMessage.error('加载用户信息失败')
      } finally {
        this.loading = false
      }
    },
    
    async updateProfile() {
      this.updateLoading = true
      try {
        const response = await userApi.updateUser(this.userInfo.userId, this.userInfo)
        if (response.success) {

          this.$store.dispatch('login', { user: this.userInfo })
          ElMessage.success('个人信息更新成功')
        }
      } catch (error) {
        console.error('更新失败:', error)
        ElMessage.error('更新失败')
      } finally {
        this.updateLoading = false
      }
    },
    
    async changePassword() {
      try {
        await this.$refs.passwordFormRef.validate()
        this.passwordLoading = true
        
        const response = await userApi.changeUserPassword(
          this.userInfo.userId, 
          this.passwordForm.newPassword
        )
        
        if (response.success) {
          ElMessage.success('密码修改成功')
          this.passwordForm = {
            newPassword: '',
            confirmPassword: ''
          }
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('密码修改失败:', error)
          ElMessage.error('密码修改失败')
        }
      } finally {
        this.passwordLoading = false
      }
    },
    
    getUserTypeText(userType) {
      const typeMap = {
        1: '系统管理员',
        2: '客户',
        3: '服务人员'
      }
      return typeMap[userType] || '未知'
    }
  }
}
</script>

<style scoped>
.profile {
  padding: 0;
}

.page-header {
  margin-bottom: 30px;
}

.page-header h2 {
  margin: 0 0 5px 0;
  color: #303133;
  font-size: 28px;
}

.page-header p {
  margin: 0;
  color: #606266;
  font-size: 14px;
}

.profile-card {
  height: 100%;
}

.profile-avatar {
  text-align: center;
  padding: 20px;
}

.profile-avatar h3 {
  margin: 15px 0 5px 0;
  color: #303133;
}

.profile-avatar p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.info-card h3 {
  margin: 0 0 20px 0;
  color: #303133;
  border-bottom: 1px solid #e4e7ed;
  padding-bottom: 10px;
}
</style> 